<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/4 11:51
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Collect_Model extends CI_Model
{
    public $error = '';

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function setError($error = '')
    {
        $this->error = $error;
    }


    /**
     * 收藏 / 取消收藏
     * @param $userid
     * @param $goodsId
     * @param $action
     * @return bool
     */
    public function doCollect($userid, $goodsId, $action)
    {
        $where = array(
            'userId' => $userid,
            'goodsId' => $goodsId
        );

        $db = clone($this->db);
        $q = $db->get_where('user_collect', $where);
        $row = $q->row_array();

        if ($action == 'off') {
            if (empty($row)) {
                return true;
            }

            // 取消
            $this->db->where(array('userId' => $userid, 'goodsId' => $goodsId));
            $this->db->delete('user_collect');
            $affected = $this->db->affected_rows();
        } else {
            if (!empty($row)) {
                return true;
            }
            // 添加
            $this->db->insert('user_collect', $where);
            $affected = $this->db->insert_id();
        }

        return $affected;
    }

    /**
     * 删除一条
     * @param $userID
     * @param $goodsID
     * @return mixed
     */
    public function deleteItem($userID, $goodsID)
    {
        $db = clone($this->db);
        $db->where(['userId' => $userID, 'goodsId' => $goodsID]);
        $db->delete('user_collect');
        return $db->affected_rows();
    }

    /**
     * 获取收藏列表
     * @param int $userid
     * @param $offset
     * @param int $limit
     * @return array|bool
     */
    public function getCollectGoods($userid, $offset = 0, $limit = 20)
    {
        $where = array('userId' => $userid);
        try {
            $db = clone($this->db);
            $db->where($where);
            $total = $db->count_all_results('user_collect');
            if ($total == 0) {
                return false;
            }

            $q = $this->db->select('g.id as goodsId, g.goodsName, g.description, g.minPrice, g.maxPrice, g.stock, g.sold, g.thumb, g.stars, g.status')
                ->from('base_goods as g')
                ->join('user_collect as uc', 'uc.goodsId = g.id')
                ->where($where)
                ->limit($limit, $offset)
                ->order_by('uc.created', 'DESC')
                ->get();
            $rows = $q->result_array();
            if (empty($rows)) {
                return false;
            }

            $collects = [];
            foreach($rows as &$item) {
                $collects[] = [
                    'goodsId' => intval($item['goodsId']),
                    'goodsName' => value($item, 'goodsName', ''),
                    'minPrice' => money_format('%i', $item['minPrice'] / 100),
                    'maxPrice' => money_format('%i', $item['maxPrice'] / 100),
                    'thumb' => thumb($item['thumb'], 200, 200),
                    'stars' => intval($item['stars']),
                    'status' => intval($item['status']),
                    'description' => value($item, 'description', ''),
                    'stock' => intval($item['stock']),
                    'sold' => intval($item['sold'])
                ];
            }

            return array(
                'total' => $total,
                'data' => $collects
            );

        } catch (PDOException $e) {
            $this->setError('获取失败');
        }

        return false;
    }

}
